package cn.tedu.medicalappointmentsystemk.controller;

import cn.tedu.medicalappointmentsystemk.pojo.bo.*;
import cn.tedu.medicalappointmentsystemk.pojo.param.DepartmentAddParam;
import cn.tedu.medicalappointmentsystemk.pojo.param.DepartmentUpdateParam;
import cn.tedu.medicalappointmentsystemk.pojo.po.PageData;
import cn.tedu.medicalappointmentsystemk.response.JsonResult;
import cn.tedu.medicalappointmentsystemk.service.DepartmentService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("v1/department")
@Slf4j
@Api(tags = "科室模块")
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;

    @PostMapping("insert")
    @PreAuthorize("hasAuthority('/departments/insert')")
    @ApiOperationSupport(order = 210)
    @ApiOperation("新增科室")
    public JsonResult addDepartment(DepartmentAddParam departmentAddParam){
        log.info("开始执行新增科室功能,入参:{}",departmentAddParam);
        departmentService.addDepartment(departmentAddParam);
        log.info("执行新增科室功能结束");
        return JsonResult.ok();
    }

    @PostMapping("{userId}/update")
    @PreAuthorize("hasAuthority('/departments/update')")
    @ApiOperationSupport(order = 210)
    @ApiOperation("科室更新")
    public JsonResult updateDepartment(DepartmentUpdateParam departmentUpdateParam,@PathVariable Long userId){
        log.info("开始执行根据管理者id:[{}]更新科室功能,入参:{},",userId,departmentUpdateParam);
        departmentService.updateDepartment(departmentUpdateParam,userId);
        log.info("执行根据管理者id更新科室功能结束");
        return JsonResult.ok();
    }
    @GetMapping("{id}/select")
    @PreAuthorize("hasAuthority('/departments/select')")
    @ApiOperationSupport(order = 220)
    @ApiOperation("科室通过id查询")
    public JsonResult selectDepartmentById(@PathVariable Long id){
        log.info("开始执行通过id:[{}]查询科室功能",id);
        DepartmentSelectByIdBO bo = departmentService.selectDepartmentById(id);
        log.info("执行通过id:[{}]查询科室功能结束,出参:{}",id,bo);
        return JsonResult.ok(bo);
    }
    @GetMapping("{id}/select-level")
    @PreAuthorize("hasAuthority('/departments/select')")
    @ApiOperationSupport(order = 230)
    @ApiOperation("传入科室id的上级科室查询")
    public JsonResult selectDepartmentLevel(@PathVariable Long id){
        log.info("开始通过id:[{}]查询上级科室",id);
        List<DepartmentSelectLevelBO> bo = departmentService.selectDepartmentLevel(id);
        log.info("通过id:[{}]查询上级科室结束,出参:{}",id,bo);
        return JsonResult.ok(bo);
    }
    @GetMapping("/select-level")
    @PreAuthorize("hasAuthority('/departments/select')")
    @ApiOperationSupport(order = 240)
    @ApiOperation("上级科室查询")
    public JsonResult selectDepartmentLevel(){
        log.info("开始上级科室查询");
        List<DepartmentSelectLevelBO> bo = departmentService.selectDepartmentLevel();
        log.info("上级科室查询结束,出参:{}",bo);
        return JsonResult.ok(bo);
    }
    @GetMapping("select/pageNum={pageNum}")
    @PreAuthorize("hasAuthority('/departments/select')")
    @ApiOperationSupport(order = 250)
    @ApiOperation("全部科室查询")
    public JsonResult selectDepartment(@PathVariable Integer pageNum){
        log.info("开始执行全部科室查询功能");
        PageData<DepartmentSelectBO> pageData = departmentService.selectDepartment(pageNum);
        log.info("执行全部科室查询功能结束,出参:{}",pageData);
        return JsonResult.ok(pageData);
    }
    @PostMapping("{id}/delete")
    @PreAuthorize("hasAuthority('/departments/delete')")
    @ApiOperationSupport(order = 260)
    @ApiOperation("删除科室")
    public JsonResult deleteDepartment(@PathVariable Long id){
        log.info("开始执行根据id:[{}]删除科室功能",id);
        departmentService.deleteDepartment(id);
        log.info("执行根据id删除科室功能结束");
        return JsonResult.ok();
    }
    @GetMapping("selectDoctor/{id}")
    @PreAuthorize("hasAuthority('/departments/select')")
    @ApiOperationSupport(order = 270)
    @ApiOperation("根据科室id查询医生信息")
    public JsonResult selectDoctor(@PathVariable Long id){
        log.info("开始执行根据科室Id:[{}]查询医生信息",id);
        List<DoctorSelectTypeBO> bo = departmentService.selectDoctorByDepartmentId(id);
        log.info("执行结束,根据科室Id:{},查询医生信息:{}",id,bo);
        return JsonResult.ok(bo);
    }
    @GetMapping("/searchLastWeek/{pageNum}")
    @PreAuthorize("hasAnyAuthority('/departments/select')")
    @ApiOperationSupport(order = 280)
    @ApiOperation("查询7日内注册的科室")
    public JsonResult searchLastWeek(@PathVariable Integer pageNum){
        PageData<DepartmentSelectBO> pageData=departmentService.searchLastWeek(pageNum);
        return JsonResult.ok(pageData);
    }
    @GetMapping("/searchLastMonth/{pageNum}")
    @PreAuthorize("hasAnyAuthority('/departments/select')")
    @ApiOperationSupport(order = 290)
    @ApiOperation("查询30日内注册的科室")
    public JsonResult searchLastMonth(@PathVariable Integer pageNum){
        PageData<DepartmentSelectBO> pageData=departmentService.searchLastMonth(pageNum);
        return JsonResult.ok(pageData);
    }
    @GetMapping("/searchToday/{pageNum}")
    @PreAuthorize("hasAnyAuthority('/departments/select')")
    @ApiOperationSupport(order = 280)
    @ApiOperation("查询7日内注册的科室")
    public JsonResult searchToday(@PathVariable Integer pageNum){
        PageData<DepartmentSelectBO> pageData=departmentService.searchToday(pageNum);
        return JsonResult.ok(pageData);
    }

    @GetMapping("selectByName/{name}")
    @PreAuthorize("hasAuthority('/appointments/select')")
    @ApiOperationSupport(order = 190)
    @ApiOperation("根据患者名字查询医生相关信息(用于搜索)")
    public JsonResult selectByName(@PathVariable String name){
        log.info("开始执行依据患者姓名:[{}]查询医生信息功能",name);
        List<DepartmentSelectBO> departmentSelectBOS = departmentService.selectByName(name);
        log.info("执行依据患者姓名:[{}]查询医生信息功能结束,出参:{}",name,departmentSelectBOS);
        return JsonResult.ok(departmentSelectBOS);
    }
}
